﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于坯布检验
    /// 1.扫描布卷号，进入验布，读取生产单信息
    /// 2.输入长度，修改数据库记录
    /// 3.剪出新布卷
    /// 4.打印标签
    /// 5.疵点保存修正
    /// 6.事件记录
    /// 7.纬纱扣除
    /// 8.织轴扣除
    /// </summary>
    public partial class TWB18
    { 
        public TWB18()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBFKOPF();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
        } 
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;         
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 4);                 
            PM["RWKRS"] = KRS ;
            PM["RWLFD"] = LFD ;
            PM["RWPOS"] = POS;
            SResult=TMV.LOADERWBFK(PM);
            MergeData(SResult, true, true);
            if (TBMain.Rows.Count > 0) 
            {
                var rm = (from DataRow row in TBMain.AsEnumerable()
                          select Convert.ToInt16(row["WBFKVERSION"].ToString()))
                 .Max();
                ERVER = int.Parse(rm.ToString());
            }
            else
            {
                ERVER = 1;
            }
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count==0)
            {
                PM["ERVER"] = ERVER;
                PM["USER"] = app.User;
                PM["DATE"] = TMV.date;
                TMV.NEWWBFK(PM);
            }
            vwMaster.View.MoveCurrentToFirst();           
            base.OnEXAUFChg(vlaue);
        }
        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
        }
        public override void OnDESChg(string des)
        {
            TBMain.Clear();
            if (des == "")
                return;
            dwData = TMV.VWBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBSTKNR"] = des;
            SResult = TMV.QWBFKSTKNR(dwData);
            MergeData(SResult, true, true);
            if (TBMain.Rows.Count > 0)
            {
                var rm = (from DataRow row in TBMain.AsEnumerable()
                          select Convert.ToInt16(row["WBFKVERSION"].ToString()))
                 .Max();
                ERVER = int.Parse(rm.ToString());
            }
            else
            {
                ERVER = 1;
            }
            base.OnDESChg(des);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            ERVER++;
            PM["RWKRS"] = KRS;
            PM["RWLFD"] = LFD;
            PM["RWPOS"] = POS;
            PM["ERVER"] = ERVER;
            PM["USER"] = app.User;
            PM["DATE"] = TMV.date;
            TMV.NEWWBFK(PM);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }  
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WBFKAENDBEN"] = app.User;
            dwMaster["WBFKAENDDAT"] = TMV.date;
            TMV.UpdateWBFKOPF(dwMaster, 0);
            base.OnSave();
        }
        public override void OnDFFSCHLChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return; 
            TMV.WBFKDFFSCHL = DFFSCHL.PadRight(3);
            TMV.UpdateWBFKOPF(dwMaster);
            base.OnDFFSCHLChg();
        }
    }
}
